﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmCollectWarehouseStockIn
    /// Version:1.0
    /// Date:2024-06-27
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为药库入库采集
    /// </remarks>
    public partial class frmCollectWarehouseStockIn : DevComponents.DotNetBar.OfficeForm
    {
        public string m_strSupplierCode = "";   //单据部门编号
        public string m_strSupplierName = "";   //单据部门名称
        public string m_strSupervisionCode = "";   // 电监码
        public Boolean m_bIsRefresh = false;    // 是否刷新
        public DataTable m_dt = null;    // 数据表
        public frmCollectWarehouseStockIn()
        {
            InitializeComponent();
            this.KeyPreview = true;
            //this.TopMost = true;
        }

        #region 01 自定义方法
        /// <summary>
        /// 数据初始化
        /// </summary>
        private void _init()
        {
            txtCode1.Text = "";
            lblCode1.Text = "";
            txtCode1.Focus();
        }

        /// <summary>
        /// 查询商品明细信息
        /// </summary>
        private void _getGoodsDetail(string strCode)
        {
            // 取发票号码信息
            m_strSupervisionCode = strCode;
            try
            {
                //发票号码处理
                string strSql = "";
                strSql = string.Format("exec P_DealWarehouseStockIn 'Collect_CheckSupervisionCode','{0}'", strCode);
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    string strResult = dt.Rows[0]["Result"].ToString();
                    // 未核对成功
                    if (strResult == "0")
                    {
                        lblHint.Text = string.Format("校对提示：{0}", dt.Rows[0]["Message"].ToString());
                    }
                    else {
                        lblHint.Text = string.Format("校对提示：{0}", "采集正常");
                        if (dt.Rows[0]["GoodsCodeCheckStatus"].ToString().Equals("0"))
                        {
                            if (CommFun._messageBoxShow("该电监码未对码，是否现在对码？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                                frmGoodsCodeCheck frm = new frmGoodsCodeCheck();                               
                                frm.m_dictInfo.Add("Code", m_strSupervisionCode);
                                frm.m_dictInfo.Add("Name", dt.Rows[0]["Name"].ToString());
                                frm.m_dictInfo.Add("PackageSpec", dt.Rows[0]["PackageSpec"].ToString());
                                frm.m_dictInfo.Add("Unit", dt.Rows[0]["Unit"].ToString());
                                frm.m_dictInfo.Add("Manufacturer", dt.Rows[0]["Manufacturer"].ToString());
                                if (frm.ShowDialog() != DialogResult.OK) {
                                    return;
                                }
                            }
                            else {
                                return;
                            }
                        }
                        // 显示商品信息
                        txtSupplierName.Text = dt.Rows[0]["SupplierName"].ToString();
                        txtSupervisionCode.Text = dt.Rows[0]["SupplierCode"].ToString();
                        txtName.Text = dt.Rows[0]["Name"].ToString();
                        txtPackageSpec.Text = dt.Rows[0]["PackageSpec"].ToString();
                        txtUnit.Text = dt.Rows[0]["Unit"].ToString();
                        txtForm.Text = dt.Rows[0]["Form"].ToString();
                        txtManufacturer.Text = dt.Rows[0]["Manufacturer"].ToString();
                        txtMakeDate.Text = dt.Rows[0]["MakeDate"].ToString();
                        txtExpiredDate.Text = dt.Rows[0]["ExpiredDate"].ToString();
                        txtPackageName.Text = dt.Rows[0]["PackageName"].ToString();
                        txtPackageRatio.Text = dt.Rows[0]["PackageRatio"].ToString();
                        txtSupervisionCode.Text = dt.Rows[0]["Name"].ToString();
                        // 保存数据
                        if (_saveCode()) {
                            _saveCodeToDataTable(dt);
                        };

                        return;
                    }
                }                
            }
            catch { }
        }

        /// <summary>
        /// 保存扫码信息
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="bIsNew"></param>
        private Boolean _saveCode()
        {
            Boolean bResult = false;            
            // 保存数据
            string strSql = string.Format("exec P_DealWarehouseStockIn 'Collect_SaveSupervisionCode','{0},{1},{2}'", m_strSupervisionCode, AppInfo.m_UserInfo.m_strCode,AppInfo.m_UserInfo.m_strName);
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            string strResultKind = dt.Rows[0]["Result"].ToString();
            bResult = strResultKind.Equals("1");
            if (!bResult)
            {
                CommFun._messageBoxShow(dt.Rows[0]["Message"].ToString());
            }
            else
            {
                m_bIsRefresh = true; 
            }            
            return bResult;
        }

        private void _saveCodeToDataTable(DataTable dt)
        {
            // 01 创建表结构
            if (m_dt == null) {
                // 创建一个DataTable实例  
                m_dt = new DataTable("Info");

                m_dt.Columns.Add("SupplierName", typeof(string));
                m_dt.Columns.Add("SupervisionCode", typeof(string));
                m_dt.Columns.Add("Name", typeof(string));
                m_dt.Columns.Add("PackageSpec", typeof(string));
                m_dt.Columns.Add("Manufacturer", typeof(string));
                m_dt.Columns.Add("Unit", typeof(string));
                m_dt.Columns.Add("Form", typeof(string));
                m_dt.Columns.Add("MakeDate", typeof(string));
                m_dt.Columns.Add("ExpiredDate", typeof(string));
                m_dt.Columns.Add("PackageName", typeof(string));
                m_dt.Columns.Add("PackageRatio", typeof(string));
            }


            // 向DataTable中添加行  
            // 方法一：使用DataRow直接添加  
            DataRow row1 = m_dt.NewRow();
            row1["SupplierName"] = dt.Rows[0]["SupplierName"].ToString(); 
            row1["SupervisionCode"] = dt.Rows[0]["SupervisionCode"].ToString();
            row1["Name"] = dt.Rows[0]["Name"].ToString();
            row1["PackageSpec"] = dt.Rows[0]["PackageSpec"].ToString();
            row1["Manufacturer"] = dt.Rows[0]["Manufacturer"].ToString();
            row1["Unit"] = dt.Rows[0]["Unit"].ToString();
            row1["Form"] = dt.Rows[0]["Form"].ToString();
            row1["MakeDate"] = dt.Rows[0]["MakeDate"].ToString();
            row1["ExpiredDate"] = dt.Rows[0]["ExpiredDate"].ToString();
            row1["PackageName"] = dt.Rows[0]["PackageName"].ToString();
            row1["PackageRatio"] = dt.Rows[0]["PackageRatio"].ToString();
            m_dt.Rows.InsertAt(row1,0);
            _showList(m_dt);
        }

        private void _showList(DataTable dt)
        {
            //if (dgvMain.DataSource != null)
            //{
            //    ((DataTable)dgvMain.DataSource).Rows.Clear();
            //}
            try
            {
                txtSupplierName.Text = dt.Rows[0]["SupplierName"].ToString();
                txtSupervisionCode.Text = dt.Rows[0]["SupervisionCode"].ToString();
                txtName.Text = dt.Rows[0]["Name"].ToString();
                txtPackageSpec.Text = dt.Rows[0]["PackageSpec"].ToString();
                txtManufacturer.Text = dt.Rows[0]["Manufacturer"].ToString();
                txtUnit.Text = dt.Rows[0]["Unit"].ToString();
                txtForm.Text = dt.Rows[0]["Form"].ToString();
                txtMakeDate.Text = dt.Rows[0]["MakeDate"].ToString();
                txtExpiredDate.Text = dt.Rows[0]["ExpiredDate"].ToString();
                txtPackageName.Text = dt.Rows[0]["PackageName"].ToString();
                txtPackageRatio.Text = dt.Rows[0]["PackageRatio"].ToString();
                // 01 取数据
                string strDataGridViewTitle = "";
                string strDataGridViewWidth = "";
                strDataGridViewTitle = "配送企业,电监码,药品名称,规格,生产厂家,单位,剂型,生产日期,有效期,包装名称,包装数量";
                strDataGridViewWidth = "200,150,200,120,250,80";
                dgvMain.DataSource = dt;
                // 02 设置列名称及宽度
                CommFun._setColumnNameDataGridView_String(dgvMain, strDataGridViewTitle);
                CommFun._setColumnWidthDataGridView_String(dgvMain, strDataGridViewWidth);
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }


        #endregion

        #region  02 窗体相关事件方法
        private void frmConfig_Load(object sender, EventArgs e)
        {
            AppInfo.AnimateWindow(this.Handle, 300, AppInfo.AW_CENTER | AppInfo.AW_ACTIVE | AppInfo.AW_VER_NEGATIVE);
            _init();
        }

        private void txtInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtCode1.Text = txtCode1.Text.Trim().Replace("，", ",");// 中文','处理
                string strCode = txtCode1.Text;
                lblCode1.Text = strCode;                
                // 扫描发票
                if (strCode.Split(',').Length == 8)
                {
                    strCode = strCode.Split(',')[2] + "," + strCode.Split(',')[3];
                }
                if (strCode.Length >= 8)
                {
                    // 编码判定
                    _getGoodsDetail(strCode);
                }
                else
                {
                    CommFun._messageBoxShow("编码长度不合法！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                txtCode1.Text = "";
                txtCode1.Focus();
            }
        }

        private void frmConfirmInvoice_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        private void dataGridViewX1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 设置行序号
            DataGridViewRow CurrentRow = ((DataGridView)sender).Rows[e.RowIndex];
            CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1);
        }
    }
}
